<template>
	<view>
		<view>
			<u-search :placeholder="petType" style="margin-bottom: 50rpx;" v-model="keyword"></u-search>
		</view>
<!--  -->
		<view class="wrap">
			<u-button >{{petType}}</u-button>
			<u-waterfall v-model="flowList" ref="uWaterfall">
				<template v-slot:left="{leftList}">
					<view class="demo-warter" v-for="(item, index) in leftList" :key="index" @tap="gotoPro(item.productId,item.productByStore)">
						<u-lazy-load threshold="-450" border-radius="10" :image="item.productImgUrl"
							:index="index"></u-lazy-load>
						<view class="demo-title">
							{{item.productName}}
						</view>
						<view class="demo-price">
							{{item.productPrice}}元
						</view>
						<view class="demo-tag">
							<view class="demo-tag-owner">
								自营
							</view>
							<view class="demo-tag-text">
								放心购
							</view>
						</view>
						<view class="demo-shop">
							{{item.productByStore}}
						</view>

					</view>
				</template>
				<template v-slot:right="{rightList}">
					<view class="demo-warter" v-for="(item, index) in rightList" :key="index" @tap="gotoPro(item.productId,item.productByStore)">
						<u-lazy-load threshold="-450" border-radius="10" :image="item.productImgUrl"
							:index="index"></u-lazy-load>
						<view class="demo-title">
							{{item.productName}}
						</view>
						<view class="demo-price">
							{{item.productPrice}}元
						</view>
						<view class="demo-tag">
							<view class="demo-tag-owner">
								自营
							</view>
							<view class="demo-tag-text">
								放心购
							</view>
						</view>
						<view class="demo-shop">
							{{item.productByStore}}
						</view>

					</view>
				</template>
			</u-waterfall>
			<u-loadmore bg-color="rgb(240, 240, 240)" :status="loadStatus" @loadmore="addRandomData"></u-loadmore>
		</view>
	</view>
</template>

<script>
	import {
		store
	} from '@/uni_modules/uni-id-pages/common/store.js';
	import {
		get,
		post,
		setToken
	} from '@/utils/api.js';
	export default {
		components: {
			
		},
		data() {
			return {
                 petType:'',
				 keyword:'',
				 petList:'',
				 loadStatus: 'loadmore',
				 flowList: [
				 ],
				 list: [{}],
			}
		},
		onLoad() {
			this.petType = uni.getStorageSync('petType')
			this.onload(this.petType)
			this.addRandomData();
			uni.removeStorageSync('petType')
		},
		onReachBottom() {
			this.loadStatus = 'loading';
			// 模拟数据加载
			setTimeout(() => {
				this.addRandomData();
				this.loadStatus = 'loadmore';
			}, 1)
		},
		methods: {
			onload(petType) {
		post('/product/getProductListByType2', 
		{
		productType2:petType
		}, {
		  needToken: true, 
		  success: (res) => {
			if (res.statusCode === 200) {
			  this.list = res.data.data
			  this.flowList = res.data.data
			}
		  }
		});
			},
			gotoPro(productId,productByStore) {
				uni.setStorageSync('productId',productId)
				uni.setStorageSync('productByStore',productByStore)
				uni.redirectTo({
					url:'/pages/proDetail/proDetail'
				})
			},
			addRandomData() {
				for (let i = 0; i < 10; i++) {
					let index = this.$u.random(0, this.list.length - 1);
					// 先转成字符串再转成对象，避免数组对象引用导致数据混乱
					let item = JSON.parse(JSON.stringify(this.list[index]))
					item.id = this.$u.guid();
					this.flowList.push(item);
				}
			},
		},
	}
</script>


<style lang="scss">
	page {
		background-color: #dea01f;
	}

	.container {
		font-size: 14px;
		background-color: #fff;
		height: 100%;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
	}

	.swiper-box {
		height: 350rpx;
	}

	.swiper-item {
		display: flex;

		image {
			width: 100%;
			height: 350rpx;
		}

		video {
			width: 100%;
			height: 350rpx;
		}
	}
	.rightbox {
		display: block;
		width: 100%;
	}

	.imgleft {
		display: block;
		// margin: 20rpx 17rpx 20rpx 0;
		margin: 8px auto;
		width: 75px;
		height: 65px;
	}

	.imgright {
		width: 17px !important;
		height: 16px !important;
		padding: 0 !important;
		margin-top: 10px;
		float: right;
	}

	.sh {
		width: 100%;
		text-align: center;
		margin-top: 10rpx;
	}

	.img {
		height: 20px;
		width: 180px;
	}
</style>
<style>
	/* page不能写带scope的style标签中，否则无效 */
	page {
		background-color: rgb(240, 240, 240);
	}
</style>

<style lang="scss" scoped>
	.demo-warter {
		border-radius: 8px;
		margin: 5px;
		background-color: #ffffff;
		padding: 8px;
		position: relative;
	}
	
	.u-close {
		position: absolute;
		top: 32rpx;
		right: 32rpx;
	}
	
	.demo-image {
		width: 100%;
		border-radius: 4px;
	}
	
	.demo-title {
		font-size: 30rpx;
		margin-top: 5px;
		color: $u-main-color;
	}
	
	.demo-tag {
		display: flex;
		margin-top: 5px;
	}
	
	.demo-tag-owner {
		background-color: $u-type-error;
		color: #FFFFFF;
		display: flex;
		align-items: center;
		padding: 4rpx 14rpx;
		border-radius: 50rpx;
		font-size: 20rpx;
		line-height: 1;
	}
	
	.demo-tag-text {
		border: 1px solid $u-type-primary;
		color: $u-type-primary;
		margin-left: 10px;
		border-radius: 50rpx;
		line-height: 1;
		padding: 4rpx 14rpx;
		display: flex;
		align-items: center;
		border-radius: 50rpx;
		font-size: 20rpx;
	}
	
	.demo-price {
		font-size: 30rpx;
		color: $u-type-error;
		margin-top: 5px;
	}
	
	.demo-shop {
		font-size: 22rpx;
		color: $u-tips-color;
		margin-top: 5px;
	}
</style>